package com.itheima.evaluate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.model.common.dtos.PageResponseResult;
import com.car.model.common.dtos.ResponseResult;
import com.car.model.evaluate.dtos.AccessoriesEvaluationDto;
import com.car.model.evaluate.pojos.Appraisal;
import com.itheima.evaluate.mapper.AccessoriesEvaluationMapper;
import com.itheima.evaluate.service.AccessoriesEvaluationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AccessoriesEvaluationServiceImpl extends ServiceImpl<AccessoriesEvaluationMapper, Appraisal> implements AccessoriesEvaluationService {
    @Override
    public ResponseResult findPage(AccessoriesEvaluationDto dto) {

        //校验参数
        if (dto == null) {
            dto.checkParam();
        }

       //构建查询条件
        LambdaQueryWrapper<Appraisal> wrapper = new LambdaQueryWrapper();

        if (dto.getCustomerName() != null) {
            wrapper.eq(Appraisal::getCustomerName,dto.getCustomerName());
        }

        if (dto.getOrderStartTime() != null&&dto.getOrderEndTime()!=null) {
            wrapper.between(Appraisal::getOrderCreateTime,dto.getOrderStartTime(),dto.getOrderEndTime());
        }

        if (dto.getServiceClassName() != null) {
            wrapper.eq(Appraisal::getServiceClassName,dto.getServiceClassName());
        }

        if (dto.getStoreName() != null) {
            wrapper.eq(Appraisal::getStoreSimpleName,dto.getStoreName());
        }

        if (dto.getOrderId() != null) {
            wrapper.eq(Appraisal::getOrderId,dto.getOrderId());
        }

        IPage<Appraisal> page = page(new Page<>(), wrapper);

        PageResponseResult pageResponseResult = new PageResponseResult((int)page.getCurrent(),(int)page.getSize(),(int)page.getTotal());

        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult findOne(Integer orderId, Integer customerId) {



        Appraisal appraisal = getOne(Wrappers.<Appraisal>lambdaQuery().eq(Appraisal::getCustomerId, customerId).eq(Appraisal::getOrderId, orderId));

        return ResponseResult.okResult(appraisal);
    }
}
